Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

409
Visualizações
postgresql: relation "table_name" does not exist when trying to CREATE TABLE

I am running this code thru pgAdmin4:

CREATE TABLE product(
    code integer, 
    pname varchar(30),
    descr varchar(50),
    utype varchar(30),
    uprice float, 
    manu varchar(30),
    sid integer,
    PRIMARY KEY (code, sid),
    FOREIGN KEY (sid) REFERENCES supplier(sid)
);

CREATE TABLE branch(
    bid integer,
    bname varchar(30),
    baddress varchar(50),
    PRIMARY KEY (bid)
);

CREATE TABLE stock(
    code integer,
    bid integer,
    units float,
    PRIMARY KEY (code, bid)
);

CREATE TABLE receipt(
    bid integer,
    rdate date,
    rtime time,
    ptype varchar(30),
    total float,
    PRIMARY KEY (bid, rdate, rtime)
);

CREATE TABLE purchase(
    bid integer,
    rdate date,
    rtime time,
    code integer,
    units float,
    PRIMARY KEY (bid, rdate, rtime, code),
    check (units > 0)
);

CREATE TABLE supplier(
    sid integer,
    sname varchar(30),
    address varchar(50),
    phone numeric (9,0),
    PRIMARY KEY (sid)
);

INSERT INTO product (code, pname, descr, utype, uprice, manu, sid) VALUES
        (987, 'Tomatoes',       'Vegetable',  'Kg',  5.99,  'manufacturer1', 111),
        (876, 'Cucumbers',      'Vegetable',  'Kg',  4.99,  'manufacturer1', 222),
        (765, 'Cornflakes',     'Cornflakes', 'Box', 15.9,  'manufacturer1', 222),

INSERT INTO branch (bid, bname, baddress) VALUES
        (987, 'tal aviv', 'road 1 tel aviv'),
        (878, 'Raanana',  'road 1 raanana'),
        (767, 'Holon',    'road 1 holon');

INSERT INTO stock (code, bid, units) VALUES
        (987, 989, 50),
        (987, 878, 75),
        (987, 767, 100),

INSERT INTO receipt (bid, rdate, rtime, ptype) VALUES
        (989, '2020-3-19', '10:00', 'Cash'),
        (989, '2020-7-16', '12:30', 'Credit'),
        (989, '2020-7-15', '15:35', 'Credit'),

INSERT INTO purchase (bid, rdate, rtime, code, units) VALUES
        (989, '2020-3-18', '10:00', 987, 5),
        (989, '2020-3-18', '10:00', 876, 3),
        (989, '2020-3-18', '10:00', 543, 4),

INSERT INTO supplier (sid, sname, address, phone) VALUES
        (111, 'supplier2', 'road2 tel aviv',  111111111),
        (222, 'supplier3', 'road3 jerusalem', 222222222),
        (333, 'supplier4', 'road2 eilat',     333333333);

i am keep getting this message: ERROR: relation "supplier" does not exist SQL state: 42P01

The problem is with the foreign key in the product table (1st table), i know this because i removed it and it created all the tables with values (i removed some of the values to facilitate the writing).

Not sure if its a syntax error, i tried a couple of ways to solve it but i am clueless, any suggestions? Thanks in advance.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You have to create supplier table before product table as sid from product table references to sid in supplier table as a foreign key.

Here is the working demo.

CREATE TABLE supplier(
    sid integer,
    sname varchar(30),
    address varchar(50),
    phone numeric (9,0),
    PRIMARY KEY (sid)
);

CREATE TABLE product(
    code integer, 
    pname varchar(30),
    descr varchar(50),
    utype varchar(30),
    uprice float, 
    manu varchar(30),
    sid integer,
    PRIMARY KEY (code, sid),
    FOREIGN KEY (sid) REFERENCES supplier(sid)
);
over 4 years ago · Santiago Trujillo Relatório

0

One option is to add the constraint later (before the insert). This way you don't have to worry about the order of table creation.

ALTER TABLE product
ADD FOREIGN KEY (sid) REFERENCES supplier(sid);
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda